Turn on more accessible mode
Skip Ribbon Commands
Skip to main content
Calculating Estimated Wait Time
Estimated wait time is a statistic that is available through the handler toolstep Acd Statistics (Queue), but sometimes that value is needed outside of a handler to get through a IceLib or ICWS. These APIs don't do many calculations, they simply provide data to the consumer in the same way that the data is presented to them by the subsystems so it is up to the consumer to calculate this manually. The steps for both APIs look like this:

  • ​Connect to the server
  • Get the call attributes Eic_WorkgroupQueueTimestamp and Eic_WorkgroupName off of the call.  Eic_WorkgroupQueueTimestamp is the time (in UTC) that the call entered the workgroup.  Eic_WorkgroupName is the name of the queue it is in.
  • Start a watch on the workgroup for the average  wait time  for the previous period
  • Get the value of the wait time
  • Calculate the estimated wait time

It is important to note that this example is veryinefficient.  In this example, we are creating a new connection and a new stat watch every time we want to know the estimated wait time. A better solution would be to create a connection, setup the watch and continually poll for the statistic changes. Then you save youself a couple steps and time by already having that data in memory. A period is every 30 minutes so if you are only watching for the previous period then all you need to do is get the data on a minimum of 30 minutes shortly after the period change to stay up to date. 

A full JSFiddle is available here: ​​​​​​​​​​​​​​​​http://jsfiddle.net/glinskik/Nspk7/4/​, but lets break it down piece by piece. The method sendRequest is a helper method that wraps the XMLHttpRequest and simplifies making the calls.


First we make a connection to the server:
1
2
3
4
5
6
7
8
9
    loginData = {
        "__type": "urn:inin.com:connection:icAuthConnectionRequestSettings",
            "applicationName": APPNAME,
            "userID": userId,
            "password": password
    };

    //Connect to the server
    sendRequest("POST", "connection", loginData, function (data) {
Once we get connected, we will issue a GET on the interaction that we are curious about to get the Eic_WorkgroupQueueTimestamp and Eic_WorkgroupName call attributes
1
2
3
4
    sendRequest("GET", "/interactions/" + interactionId + "?select=Eic_WorkgroupQueueTimestamp,Eic_WorkgroupName", null, function (data) {
            //case matters
            var workgroupName = data.attributes.Eic_WorkgroupName;
            var enteredTimeStamp = data.attributes.Eic_WorkgroupQueueTimestamp;
Next we need to tell the server to start watching the wait time statistic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 var statData = [{
                "statisticIdentifier": "inin.workgroup:AverageWaitTime",
                    "parameterValueItems": [{
                    "parameterTypeId": "ININ.People.WorkgroupStats:Workgroup",
                        "value": workgroupName //From the call attribute
                }, {
                    "parameterTypeId": "ININ.Queue:Interval",
                        "value": "PreviousPeriod"
                }]
            }];

            //we need to setup the subscription for the statistics.  Here we'll tell the server that we want to watch for
            //stat changes
            sendRequest("PUT", "/messaging/subscriptions/statistics/statistic-values", {
                'statisticKeys': statData
            }, function () {
In order to give the server a chance to setup the watch, we will issue a setTimeout call to basically sleep for a second before querying the data. Then we will poll for messages from the server and parse out the stat value.
1
sendRequest("GET", "/messaging/messages", null, function (messages) {
The last thing that needs to happen is for us to calculate the wait time estimate. Eic_WorkgroupQueueTimestamp is a time in the format of 20140630T160847.663Z and is UTC. We'll convert that to a new Date and correct for GMT, then compare to the current time and post an alert depending on what the estimated time waiting is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 var now = new Date();
//Get a date from the entered string "20140630T160847.663Z"
                                        
var startTime = new Date(enteredTimeStamp.substring(0, 4),
enteredTimeStamp.substring(4, 6) - 1, //need 0 based month
enteredTimeStamp.substring(6, 8),
enteredTimeStamp.substring(9, 11),
enteredTimeStamp.substring(11, 13) - now.getTimezoneOffset(), //correct for GMT
enteredTimeStamp.substring(13, 14));

var currentTimeWaitingMs = now - startTime;
var currentTimeWaitingSec = currentTimeWaitingMs / 1000;

//convert the difference in seconds to minutes and round up.
var estimatedTimeWaiting = Math.ceil((previousPeriodWaitTime - currentTimeWaitingSec) / 60);

if (estimatedTimeWaiting < 0) {
    //Call has been waiting longer than the average,
    alert('soon');
} else if (estimatedTimeWaiting < 1) {
    alert('call will be answered in approx ' + estimatedTimeWaiting + ' minute');
} else {
    alert('call will be answered in approx ' + estimatedTimeWaiting + ' minutes');
}
​​​​​​